package vehicle;

import java.util.HashMap;
import java.util.Map;

public class MonitorVehicleTracker {

	private final Map<String,MutablePoint> locations;
	
	public MonitorVehicleTracker(Map<String,MutablePoint> data) {
		locations = deepCopy(data);
	}
	
	public synchronized MutablePoint getLocation(String name) {
		MutablePoint loc = safeGet(name);
		return new MutablePoint(loc);
	}
	
	public synchronized void setLocation(String name, MutablePoint point) {
		MutablePoint loc = safeGet(name);
		loc.x = point.x;
		loc.y = point.y;
	}

	private MutablePoint safeGet(String name) {
		MutablePoint loc = locations.get(name);
		if(loc == null) throw new IllegalArgumentException("No such Id: " + name);
		return loc;
	}
	
	public Map<String,MutablePoint> getLocations() {
		return deepCopy(locations);
	}

	private Map<String, MutablePoint> deepCopy(Map<String, MutablePoint> data) {
		Map<String, MutablePoint> copy = new HashMap<>();
		for(String id : data.keySet()) {
			MutablePoint point = data.get(id);
			copy.put(id, new MutablePoint(point));
		}
		return copy;
	}
	
}
